package edu.towson.cosc.cosc455.jsmole1.markdowncompiler;

import edu.towson.cosc.cosc455.interfaces.LexicalAnalyzer;

public class SuperHappyFuntimeLexicalAnalyzer implements LexicalAnalyzer {
	
	/** String of entire read-in file **/
	public static String bigString = "";
	
	/** Final draft of constructed token **/
	public static String token = "";
	
	/** Used for stringbuilding **/
	private String possibleToken = "";
	
	private static int index = 0;

	@Override
	public void getNextToken() {
		
		possibleToken = "";
		
		while (((((bigString.charAt(index)) + "").equals("\r"))
				|| (((bigString.charAt(index)) + "").equals("\n"))
				|| (((bigString.charAt(index)) + "").equals("\t"))))
			index++;
		
		// Token
		if ((getCharacter() + "").equalsIgnoreCase("#")) {
			
			// Two word token
			if ((nextCharacter() + "").equalsIgnoreCase("s") || (nextCharacter() + "").equalsIgnoreCase("e"))
				getTwoWordToken();
			
			// Single word token
			else
				getSingleWordToken();
			
		// Text
		} else
			getTextToken();
		
		while (possibleToken.endsWith("\t"))
			possibleToken = possibleToken.substring(0, possibleToken.indexOf("\t"));
		
		token = possibleToken.trim();
		
		// If trimming caused token to be blank, then we didn't actually get a token, but moved index to the actual one
		if (possibleToken.equals(""))
			getNextToken();
		
		if (SuperHappyFuntimeCompiler.debugStatements)
			System.out.println("--" + token + "--"); // -- symbols to tell if/where spaces exist
	}

	@Override
	public char getCharacter() {
		return bigString.charAt(index);
	}
	
	/** Gets the character one after the current index. Used for check the first letter of a tag
	 * @return single value String */
	public String nextCharacter() {
		return bigString.charAt(index + 1) + "";
	}

	@Override
	public void addCharacter() {
		possibleToken += bigString.charAt(index);
	}

	@Override
	public boolean isSpace() {
		if (((bigString.charAt(index)) + "").equals(" "))
			return true;
		
		return false;
	}

	@Override
	public boolean lookupToken(String c) {
		for (int i = 0; i < Tag.tags.size(); i++) {
			if (Tag.tags.get(i).equalsIgnoreCase(token.toUpperCase().trim()))
				return true;
		}
		
		return false;
	}
	
	/** Gets a double word token **/
	public void getTwoWordToken() {
		
		boolean wentThroughWhile = false;
		int spaceDelimeter = 0;
		
		while (spaceDelimeter < 2) {
			
			while (((((bigString.charAt(index)) + "").equals("\r"))
					|| (((bigString.charAt(index)) + "").equals("\n"))
					|| (((bigString.charAt(index)) + "").equals("\t")))) {
				index++;
				wentThroughWhile = true;
			}
			
			if (wentThroughWhile == false)
				addCharacter();
			else
				spaceDelimeter = 2;	// Kill the loop
			
			
			if (isSpace())
				spaceDelimeter++;
			index++;
			
			// and go back just one more
			if (wentThroughWhile == true)
				index--;
		}
			if (!lookupToken(possibleToken.trim())) { // If not valid token
				if (SuperHappyFuntimeCompiler.debugStatements)
					System.out.println("Not valid token");
			}
	}
	
	/** Gets a single word token (#name, #value, #newline) **/
	public void getSingleWordToken() {
		
		int spaceDelimeter = 0;
		
		while (spaceDelimeter < 1) {
			addCharacter();
			if (isSpace())
				spaceDelimeter++;
			index++;
		}
	}
	
	/** Gets a text based token **/
	public void getTextToken() {
		// Take care of leading white space
		while (((((bigString.charAt(index)) + "").equals("\r"))
				|| (((bigString.charAt(index)) + "").equals("\n"))
				|| (((bigString.charAt(index)) + "").equals("\t"))
				|| isSpace())) {
			index++;
		}
		while (!(bigString.charAt(index) + "").equals("#")) {
			possibleToken += bigString.charAt(index);
			index++;
		}
	}

}
